﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
using System.Windows;
using NAudio.Wave;
using System.Drawing;
using System.Diagnostics;
using System.IO;

namespace T_player
{
    public partial class Tplayer : Form
    {
        private string mediaName;
        private string safeMediaNeme;
        private bool firstTimeMediaLoaded;
        private Timer timer;
        private Stopwatch stopWatch;
        private Point point, volumePoint;
        private float volBeforeMute;

        public Tplayer() {
            InitializeComponent();
            firstTimeMediaLoaded = false;
            volumePoint.Y = 50;
            this.StartPosition = FormStartPosition.CenterScreen;
            mediaInfo.BackColor = Color.Transparent;
            loadPictureBox.BackColor = Color.Transparent;
            play_pausePictureBox.BackColor = Color.Transparent;
            stopPictureBox.BackColor = Color.Transparent;
            mix_maxPictureBox.BackColor = Color.Transparent;
            exitPictureBox.BackColor = Color.Transparent;
            volumeImage.BackColor = Color.Transparent;

            if (PlayMedia.clickedFileName != null) {
                mediaName = PlayMedia.clickedFileName;
                firstTimeMediaLoaded = true;
                mediaInfo.Text = safeMediaNeme = Path.GetFileName(PlayMedia.clickedFileName);
            }
        }

        private void loadButton_Click(object sender, EventArgs e) {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            ofd.Filter = "MP3 file (*.mp3)|*.mp3";
            if (ofd.ShowDialog() != DialogResult.OK) return;

            mediaName = ofd.FileName;
            firstTimeMediaLoaded = true;
            mediaInfo.Text = safeMediaNeme = ofd.SafeFileName;
        }

        private void play_pauseButton_Click(object sender, EventArgs e) {
            if (firstTimeMediaLoaded) {
                PlayMedia.playMP3(mediaName);
                PlayMedia.audioFileReader.Volume = (float)(100 - volumePoint.Y) / 100;

                durationLabel.Text = PlayMedia.audioFileReader.TotalTime.ToString();
                stopWatch = new Stopwatch();
                timer = new Timer();
                timer.Interval = Convert.ToInt32(PlayMedia.audioFileReader.TotalTime.TotalMilliseconds / 400); //media duration over 400 px --> time to move one px
                timer.Tick += new EventHandler(timer_Tick);
                updateCurrentTimeLabelTextAndMoveTriangle();

                firstTimeMediaLoaded = false;
                this.Text = "T player | Playing " + safeMediaNeme;

                play_pausePictureBox.Image = T_player.Properties.Resources.pause;
            }
            else if (PlayMedia.waveOutDevice != null) {
                if (PlayMedia.waveOutDevice.PlaybackState == PlaybackState.Playing) {
                    timer.Stop();
                    PlayMedia.waveOutDevice.Pause();
                    this.Text = "T player | Paused " + safeMediaNeme;
                    mediaInfo.Text = safeMediaNeme;

                    play_pausePictureBox.Image = T_player.Properties.Resources.play;
                }
                else if (PlayMedia.waveOutDevice.PlaybackState == PlaybackState.Paused) {
                    timer.Start();
                    PlayMedia.waveOutDevice.Play();
                    PlayMedia.audioFileReader.Volume = (float)(100 - volumePoint.Y) / 100;
                    updateCurrentTimeLabelTextAndMoveTriangle();
                    this.Text = "T player | Playing " + safeMediaNeme;
                    mediaInfo.Text = safeMediaNeme;
                    
                    play_pausePictureBox.Image = T_player.Properties.Resources.pause;
                }
            }
        }
        
        private void stopButton_Click(object sender, EventArgs e) {
            if (PlayMedia.waveOutDevice != null) {
                if (PlayMedia.waveOutDevice.PlaybackState == PlaybackState.Playing) {
                    PlayMedia.waveOutDevice.Stop();
                }
            }
            if (PlayMedia.audioFileReader != null) { PlayMedia.audioFileReader.Position = Convert.ToInt64(0); }
            point.X = 0;
            if (timer != null) { timer.Dispose(); }
            mediaInfo.Text = "No media sellected...";
            paparitsa.Location = point;
            currentTimeLabel.Text = "00:00";
            durationLabel.Text = "--:--";
            play_pausePictureBox.Image = T_player.Properties.Resources.play;
        }

        private void min_maxButton_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized) {
                this.WindowState = FormWindowState.Normal;
            }
            if (this.WindowState == FormWindowState.Normal) {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        private void exitButton_Click(object sender, EventArgs e) { Application.Exit(); }

        private void volumePanel_Click(object sender, EventArgs e) {
            if (PlayMedia.audioFileReader != null)
            {
                volumePoint = volumePanel.PointToClient(Cursor.Position);
                volumePanel.Invalidate();
                PlayMedia.audioFileReader.Volume = (float)(100 - volumePoint.Y) / 100;

                volumeImage.Image = T_player.Properties.Resources.vol;
            }
        }

        private void volumePanel_Paint(object sender, PaintEventArgs e) {
            base.OnPaint(e);
            
            using (Graphics g = e.Graphics)
            {
                e.Graphics.ScaleTransform(1.0f, -1.0f);
                e.Graphics.TranslateTransform(0, -volumePanel.Size.Height);
                g.FillRectangle(new SolidBrush(Color.LightGreen), 0, 0, 17, volumePanel.Size.Height - volumePoint.Y);
            }
        }

        private void volumePanelImage_Click(object sender, EventArgs e) {
            if (PlayMedia.audioFileReader != null) {
                if (PlayMedia.audioFileReader.Volume != 0.0f)
                {
                    volBeforeMute = PlayMedia.audioFileReader.Volume;
                    PlayMedia.audioFileReader.Volume = 0.0f;
                    volumeImage.Image = T_player.Properties.Resources.muted;
                }
                else
                {
                    PlayMedia.audioFileReader.Volume = volBeforeMute;
                    volumeImage.Image = T_player.Properties.Resources.vol;
                }
            }
        }

        private void updateCurrentTimeLabelTextAndMoveTriangle() {
            timer.Start();
            stopWatch.Start();
        }
            
        private void timer_Tick(object sender, EventArgs e) {
            currentTimeLabel.Text = PlayMedia.audioFileReader.CurrentTime.ToString().Substring(0, 8);

            point.X = point.X + 1;
            point.Y = 25;
            paparitsa.Location = point;
            if (stopWatch.ElapsedMilliseconds >= PlayMedia.audioFileReader.TotalTime.TotalMilliseconds) {
                stopWatch.Stop();
                timer.Stop();
            }
            if (PlayMedia.waveOutDevice.PlaybackState == PlaybackState.Stopped) {
                PlayMedia.audioFileReader.Position = Convert.ToInt64(0);
                point.X = 0;
                timer.Stop();
                paparitsa.Location = point;
            }
        }

        private void currentTime_Click(object sender, EventArgs e) {
            point = proccessPanel.PointToClient(Cursor.Position);
            point.Y = 25;

            if (PlayMedia.audioFileReader != null) {
                currentTimeLabel.Text = TimeSpan.FromMilliseconds(point.X * Convert.ToInt64(PlayMedia.audioFileReader.TotalTime.TotalMilliseconds / 400)).ToString().Substring(0, 8);
                PlayMedia.audioFileReader.Position = Convert.ToInt64(Math.Round((PlayMedia.audioFileReader.Length * point.X * Math.Round(PlayMedia.audioFileReader.TotalTime.TotalMilliseconds / 400, 2, MidpointRounding.AwayFromZero) / PlayMedia.audioFileReader.TotalTime.TotalMilliseconds), 2, MidpointRounding.AwayFromZero));
            }
            if (PlayMedia.audioFileReader != null) { paparitsa.Location = point; }
        }
    }
}